package com.xinnet.mms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.product.service.ProductService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/ruleSet/intentionCustomBrokerageSet")
public class IntentionCustomBrokerageSetController {
	
	@Autowired
	ProductService productService;
	
	
	/**
	 * 跳转到意向客户分成页面
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping(value = "/intentionCustomBrokerageSetPage")
	public String intentionCustomBrokerageSetPage(HttpServletRequest request,HttpServletResponse response){
		List<Product> productList = productService.queryEnableProduct();
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(productList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(productList.size(), productList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,productList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),productList);
		}
		request.setAttribute("pageView", pageView);
		return "/ruleSet/intentionCustomBrokerageSet";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/updateSingleRate")
	public String updateSingrequestleRate(HttpServletRequest request,HttpServletResponse response){
		String singleRateStr = request.getParameter("singleRate");
		Double singleRate = Double.parseDouble(singleRateStr);
		productService.updateSingleRate(singleRate);
		return "redirect:/ruleSet/intentionCustomBrokerageSet/intentionCustomBrokerageSetPage";
	}
	
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/updateBrokerage")
	public String updateBrokerage(HttpServletRequest request,HttpServletResponse response){
		String productId = request.getParameter("productId");
		String singlePushMoney = request.getParameter("singlePushMoney");
		String isUseSingleRateForPushMoney = request.getParameter("isUseSingleRateForPushMoney");
		String divide = request.getParameter("divide");
		String isUseSingleRateForPushDivided = request.getParameter("isUseSingleRateForPushDivided");
		String singleRate = request.getParameter("singleRate");
		Product product = new Product();
		product.setId(productId);
		if(!"".equals(singlePushMoney)){
			product.setSinglePushMoney(Double.parseDouble(singlePushMoney));
		}
		if(!"".equals(isUseSingleRateForPushMoney)){
			product.setIsUseSingleRateForPushMoney(isUseSingleRateForPushMoney);
		}
		if(!"".equals(divide)){
			product.setDivide(Double.parseDouble(divide));
		}
		if(!"".equals(isUseSingleRateForPushDivided)){
			product.setIsUseSingleRateForPushDivided(isUseSingleRateForPushDivided);
		}
		product.setIsEffectCustomDivided("2");
		product.setSingleRate(Double.parseDouble(singleRate));
		productService.updateProduct(product);
		return "redirect:/ruleSet/intentionCustomBrokerageSet/intentionCustomBrokerageSetPage";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathUpdateBrokerage")
	public String bathUpdateBrokerage(HttpServletRequest request,HttpServletResponse response){
		String singleRate = request.getParameter("singleRate");
		String productIdStr = request.getParameter("productIdStr");
		String singlePushMoney = request.getParameter("singlePushMoney");
		String isUseSingleRateForPushMoney = request.getParameter("isUseSingleRateForPushMoney");
		String divide = request.getParameter("divide");
		String isUseSingleRateForPushDivided = request.getParameter("isUseSingleRateForPushDivided");
		String[] productIdArray = productIdStr.split(",");
		for (int i = 0; i < productIdArray.length; i++) {
			String productId = productIdArray[i];
			Product product = new Product();
			product.setId(productId);
			if(!"".equals(singlePushMoney)){
				product.setSinglePushMoney(Double.parseDouble(singlePushMoney));
			}
			if(!"".equals(isUseSingleRateForPushMoney)){
				product.setIsUseSingleRateForPushMoney(isUseSingleRateForPushMoney);
			}
			if(!"".equals(divide)){
				product.setDivide(Double.parseDouble(divide));
			}
			if(!"".equals(isUseSingleRateForPushDivided)){
				product.setIsUseSingleRateForPushDivided(isUseSingleRateForPushDivided);
			}
			product.setIsEffectCustomDivided("2");
			product.setSingleRate(Double.parseDouble(singleRate));
			productService.updateProduct(product);
		}
		return "redirect:/ruleSet/intentionCustomBrokerageSet/intentionCustomBrokerageSetPage";
	}
}
